$(function () {
    obj.init();
});

var obj = new function() {
    return {
        showMsg: '',
        img: '',
        init: function () {
            this.showMsg = $('#showMsg');
            this.listener();
        },
        listener: function () {
            var $this = this;
            //点击保存
            $this.showMsg.on('click','[id^="confirmId_"]',function () {
                var id = $(this).attr("id").split("confirmId_")[1];
                $this.funObj().saveCheckMsg(id);
            });
            //继续添加
            $this.showMsg.on('click','[id^="addMsg_"]',function () {
                var id = $(this).attr("id").split("addMsg_")[1];
                $this.funObj().addCheckMsg(id);
            });
        },
        funObj: function () {
            var $this = this;
            return {
                saveCheckMsg: function (_id) {
                    if (!_id || _id === undefined) {
                        window.top.xhTop.showError('缺少参数');
                        return false;
                    }
                    var checkMsg = $('#msgId_'+_id).val();
                    if (!checkMsg) {
                        window.top.xhTop.showError('请填写信息');
                        return false;
                    }
                    xhExtra.askChangeStatus('res/setCheckMsg', {id: _id, checkMsg: checkMsg}, null, '确认保存？', function (_data) {
                        if (_data.code === 200) {
                            location.reload();
                        }
                    });
                },
                addCheckMsg: function (_id) {
                    if (!_id || _id === undefined) {
                        window.top.xhTop.showError('缺少参数');
                        return false;
                    }
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '追加确认信息',
                        html: '<div><textarea id="checkMsg" class="swal2-textarea" placeholder="必填">',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var checkMsg     = $('#checkMsg', window.top.document).val();
                                if (checkMsg < 1){
                                    reject('确认信息不能为空');
                                } else {
                                    xhExtra.ajaxRequest('res/setCheckMsg', {
                                        id: _id,
                                        checkMsg: checkMsg
                                    }, {success: true}, function (_data) {
                                        if (_data.code === 200) {
                                            location.reload();
                                        }
                                    });
                                    resolve();
                                }
                            })
                        }
                    })
                }
            }
        }
    }
};
